/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.to.aboomy.bannersample.indicator;

import com.to.aboomy.banner.Indicator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

/**
 * DashReverseView指示器
 *
 * @since 2021-08-05
 */
public class DashReverseView extends DirectionalLayout implements Indicator {
    private static final int NUM_2 = 2;
    private static final int NUM_6 = 6;
    private static final int NUM_20 = 20;
    private static final int NUM_80 = 80;
    private static final int NUM_180 = 180;
    private static final int NUM_300 = 300;
    DependentLayout.LayoutConfig params = null;
    private int pointRadius = NUM_6;
    private int pointSelectColor = Color.RED.getValue();
    private int pointColor = Color.GRAY.getValue();
    private int pointWidth = pointRadius * NUM_6;
    private int pointHeight = pointRadius * NUM_2;
    private int spacing = pointRadius;

    /**
     * 构造
     *
     * @param context
     */
    public DashReverseView(Context context) {
        super(context);
        setOrientation(DirectionalLayout.HORIZONTAL);
    }

    @Override
    public void initIndicatorCount(int pagerCount) {
        setVisibility(pagerCount > 1 ? VISIBLE : INVISIBLE);
        removeAllComponents();
        DirectionalLayout.LayoutConfig lp = new DirectionalLayout.LayoutConfig(
                LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        lp.setMarginLeft(spacing);
        lp.setMarginRight(spacing);
        lp.setMarginTop(NUM_20);
        for (int i1 = 0; i1 < pagerCount; i1++) {
            // 翻页指示的点
            Text pointView = new Text(getContext());
            pointView.setLayoutConfig(lp);
            ShapeElement drawable = new ShapeElement();
            drawable.setCornerRadiiArray(new float[]{pointRadius, pointRadius,
                pointRadius, pointRadius, pointRadius, pointRadius, pointRadius, pointRadius});
            pointView.setWidth(pointWidth);
            int temp = 0;
            if (temp == i1) {
                drawable.setRgbColor(RgbColor.fromArgbInt(pointSelectColor));
                pointView.setSelected(true);
            } else {
                drawable.setRgbColor(RgbColor.fromArgbInt(pointColor));
                pointView.setSelected(false);
            }
            pointView.setHeight(pointHeight);
            pointView.setBackground(drawable);
            addComponent(pointView);
        }
    }

    @Override
    public Component getView() {
        return this;
    }

    @Override
    public DependentLayout.LayoutConfig getParams() {
        if (params == null) {
            params = new DependentLayout.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_CONTENT, NUM_80);
            params.addRule(DependentLayout.LayoutConfig.ALIGN_PARENT_BOTTOM);
            params.addRule(DependentLayout.LayoutConfig.HORIZONTAL_CENTER);
            params.addRule(DependentLayout.LayoutConfig.CENTER_IN_PARENT);
            params.addRule(DependentLayout.LayoutConfig.BELOW);
        }
        return params;
    }

    @Override
    public void setAutoStau(Boolean isAuto) {
    }

    @Override
    public void onPageSliding(int i, float v, int i1) {
    }

    @Override
    public void onPageSlideStateChanged(int i) {
    }

    @Override
    public void onPageChosen(int position) {
        selectPoint(position);
    }

    /**
     * selectPoint
     *
     * @param position
     */
    public void selectPoint(int position) {
        final int count = getChildCount();
        if (position >= 0 && position < count) {
            Text pointView;
            for (int i1 = 0; i1 < count; i1++) {
                pointView = (Text) getComponentAt(i1);
                if (position == i1) {
                    selectPoint(pointView);
                } else {
                    unSelectPoint(pointView);
                }
            }
        }
    }

    private void selectPoint(final Text v) {
        if (v != null && !v.isSelected()) {
            final ShapeElement drawable = (ShapeElement) v.getBackgroundElement();
            drawable.setRgbColor(RgbColor.fromArgbInt(pointSelectColor));
            AnimatorValue animatorValue = new AnimatorValue();
            animatorValue.setDuration(NUM_300);
            animatorValue.setValueUpdateListener((animatorValue1, v1) -> {
                v.setRotation(NUM_180 * v1);
            });
            animatorValue.start();
            v.setSelected(true);
        }
    }

    private void unSelectPoint(final Text v) {
        if (v != null && v.isSelected()) {
            final ShapeElement drawable = (ShapeElement) v.getBackgroundElement();
            drawable.setRgbColor(RgbColor.fromArgbInt(pointColor));
            v.setSelected(false);
        }
    }
}
